﻿function AjaxRequest()
{}

AjaxRequest.m_MaxPool = 50;
AjaxRequest.m_Pools = [];

AjaxRequest.___GetPooledXmlHttp = function()
{
    var xmlhttp = null;
    var pools = this.m_Pools;
    for( var ii=0; ii<pools.length; ++ii )
    {
        var pool = pools[ii];
        if( pool.readyState == 4 || pool.readyState == 0 )
        {
            xmlhttp = pool;
            break;
        }
    }
    
    if( xmlhttp ) return xmlhttp;
    
    return this.___CreateXmlHttp();
};

AjaxRequest.___CreateXmlHttp = function()
{
    if( this.m_Pools.length < this.m_MaxPool )
    {
        var xmlhttp = null;
        //IE 
        try 
        { 
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
        } 
        catch(e) 
        { 
            try 
            { 
                xmlhttp = new ActiveXObject('MSXML2.XMLHTTP'); 
            } 
            catch(ee) {} 
        } 
       
        if( !xmlhttp )
        {
            //Mozilla and Safari  
            if( typeof(XMLHttpRequest) != 'undefined' )
            {
                try
                {
                    xmlhttp = new XMLHttpRequest();               
                }   
                catch(e) {} 
            }  
           
            //IceBrowser   
            if( !xmlhttp &&  window.createRequest )
            {
                try
                {  
                    xmlhttp = window.createRequest();  
                }
                catch(e) {};  
            }   
        }
        
        if ( xmlhttp ) 
        { 
            this.m_Pools.push(xmlhttp); 
        } 
        return xmlhttp; 
    }
};

AjaxRequest.___GetXmlDom = function()
{
    var xmlDoc = null;
    try
    {
        xmlDoc = new ActiveXObject("msxml2.xmldom" );
    }
    catch(e)
    {   
        try
        {
            xmlDoc = new ActiveXObject("Microsoft.xmldom");
        }
        catch(e)
        {
            try
            {
                xmlDoc = new ActiveXObject("MSXML.xmldom" );
            }
            catch(e)
            {
               try
               {
                    xmlDoc = new ActiveXObject("MSXML3.xmldom" );
               }
               catch(e)
               {
                    
               }
            }  
        }  
    }
    
    return xmlDoc;
};

AjaxRequest.___RequestDataToCallback = function( url, callback, method, data )
{
    var xmlhttp = this.___GetPooledXmlHttp();
    if( !xmlhttp ) return null;
    
    xmlhttp.open(method, url, true);
    xmlhttp.onreadystatechange = function()
    {
        if( xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete' )
        {
            callback(xmlhttp.responseText);
        }
    };
    xmlhttp.send(data);
};

AjaxRequest.___RequestDataOnly = function( url, method, data )
{
	var xmlhttp = this.___GetPooledXmlHttp();
    if ( !xmlhttp ) return null;

    if ( method == 'POST' )
    {
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    }
    
    xmlhttp.open(method, url, false);
    try
    {
        xmlhttp.send(data);
    }
    catch(e)
    {}

    if ( xmlhttp.status == 200 )
    {
        return xmlhttp.responseText;
    }
    
    return '';
};

AjaxRequest.___BuildWebServicesSoap = function(dataList, fnName)
{
    var data;
    data = '<?xml version="1.0" encoding="utf-8"?>\r\n'; 
    data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\r\n'; 
    data = data + '<soap:Body>\r\n'; 
    data = data + '<' + fnName + ' xmlns="http://fund123.cn/">\r\n';
    for( var i=0; i< dataList.length; i+=2 )
    { 
        data = data + '<' + dataList[i] + '>'+dataList[i+1]+'</' + dataList[i] + '>\r\n'; 
    } 
    data = data + '</' + fnName + '>\r\n'; 
    data = data + '</soap:Body>\r\n'; 
    data = data + '</soap:Envelope>\r\n';
    
    return data;
};

AjaxRequest.___RequestWebServicesCallback = function( url, fnName, dataList, callback )
{
    var xmlhttp = this.___GetPooledXmlHttp();
    if( !xmlhttp ) return null;
    xmlhttp.open("POST", url, true);
    
    dataList = this.___BuildWebServicesSoap(dataList, fnName);
    
    xmlhttp.setRequestHeader('Content-type','text/xml; charset=utf-8');
    xmlhttp.setRequestHeader('Content-Length', dataList.length);
    xmlhttp.setRequestHeader("SOAPAction", "http://fund123.cn/" + fnName);        

    xmlhttp.onreadystatechange = function()
    {
        if( xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete' )
        {
            var responseText = xmlhttp.responseText
            var xmldom = AjaxRequest.___GetXmlDom();
            xmldom.async = false;
            xmldom.loadXML(responseText);
            callback(xmldom.text);
        }
    };

    try
    {
        xmlhttp.send(dataList);
    }
    catch(e)
    {}
        
    return '';
    
};

AjaxRequest.___RequestWebServicesData = function(  url, fnName, dataList )
{
    var xmlhttp = this.___GetPooledXmlHttp();
    if( !xmlhttp ) return null;
    xmlhttp.open("POST", url, true);
    
    dataList = this.___BuildWebServicesSoap(dataList, fnName);
    
    xmlhttp.setRequestHeader('Content-type','text/xml; charset=utf-8');
    xmlhttp.setRequestHeader('Content-Length', dataList.length);
    xmlhttp.setRequestHeader("SOAPAction", "http://fund123.cn/" + fnName);        

    try
    {
        xmlhttp.send(dataList);
    }
    catch(e)
    {}
    
    if ( xmlhttp.status == 200 )
    {
        var responseText = xmlhttp.responseText
        var xmldom = AjaxRequest.___GetXmlDom();
        xmldom.async = false;
        return xmldom.text;
    }
    
    return '';
};

AjaxRequest.RequestWebServicesCallback = function( url, fnName, dataList, callback  )
{
    this.___RequestWebServicesCallback(url, fnName, dataList, callback);
};

AjaxRequest.RequestWebServicesData = function( url, fnName, dataList )
{
    return this.___RequestWebServicesData(url, fnName, dataList);
};

AjaxRequest.CallbackByGet = function( url, callback )
{
    return this.___RequestDataToCallback(url, callback, 'GET', null);
};

AjaxRequest.CallbackByPost = function( url, callback, data )
{
    return this.___RequestDataToCallback(url, callback, 'POST', data);
};

AjaxRequest.RequestDataByGet = function( url )
{
    return this.___RequestDataOnly(url, 'GET', null);
};

AjaxRequest.RequestDataByPost = function( url, data )
{
    return this.___RequestDataOnly(url, 'POST', data);
};

AjaxRequest.AbortAll = function()
{
    var tmpCreateMethod = this.___CreateXmlHttp;
    this.___CreateXmlHttp = function()
    { return null; }
    
    for( var i=0; i<this.m_Pools.length; ++i )
    {
        this.m_Pools[i].abort();
    }
    
    this.___CreateXmlHttp = tmpCreateMethod;
};